import './assets/base.css'
import './assets/common.css'

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

const app = createApp(App)

// ElementPlus
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
app.use(ElementPlus)

import axios from 'axios'
axios.defaults.baseURL = 'http://localhost:8080'

// // 加入请求拦截器
axios.interceptors.request.use(config => {
  // 获取token
  const token = localStorage.getItem("token");
  // console.log(token);
  // 判断token是否存在
  if(token){
    // 如果存在，将token添加到请求头中
    config.headers.token = token;
  }
  // console.log("Request headers:", config.headers.token)
  return config;
})

// 加入响应拦截器
axios.interceptors.response.use(res => {
  if(res.data.code == 401){
    router.push("/login");
  }else{
    return res;
  }
})
// 如果您正在使用CDN引入，请删除下面一行。
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.use(router)
app.use(axios)
app.mount('#app')
